package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.BluePlateInformation;
import com.smart.village.big.screen.mapper.ChangeAgricultureMapper;
import com.smart.village.big.screen.service.ChangeAgricultureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChangeAgricultureServiceImpl implements ChangeAgricultureService {
    @Autowired
    private ChangeAgricultureMapper changeAgricultureMapper;

    @Override
    public List<JSONObject> getIndustrialStructureStatics(String year, String townNumber, String villageNumber) {
        return changeAgricultureMapper.getIndustrialStructureStatics(year, townNumber, villageNumber);
    }

    @Override
    public JSONObject getImportantAgricultureProjectList(String year, String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> projectList = changeAgricultureMapper.getImportantAgricultureProjectList(year, townNumber, villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(projectList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getSubsidyInformation(String year, String townNumber, String villageNumber) {
        JSONObject jsonObject = new JSONObject();
        int policyCnt = changeAgricultureMapper.getSubsidyInformationPolicyCnt(year);
        int machineryPolicyCnt = changeAgricultureMapper.getSubsidyMachineryInformationPolicyCnt(year);
        int policyArea = changeAgricultureMapper.getSubsidyInformationPolicyArea(year, townNumber, villageNumber);
        int mainBodyCnt = changeAgricultureMapper.getSubsidyInformationMainBodyCnt(year, townNumber, villageNumber);
        int mainBodyMachineryCnt = changeAgricultureMapper.getSubsidyMachineryInformationMainBodyCnt(year, townNumber, villageNumber);
        List<JSONObject> subsidyInformationBottomList = changeAgricultureMapper.getSubsidyInformationBottomChartData(year, townNumber, villageNumber);
        List<JSONObject> subsidyMachineryInformationBottomList = changeAgricultureMapper.getSubsidyMachineryInformationBottomChartData(year, townNumber, villageNumber);

        List<String> Month = new ArrayList<>();
        List<String> Amount = new ArrayList<>();
        List<String> Amount2 = new ArrayList<>();
        List<String> Area = new ArrayList<>();
        List<String> Hu2 = new ArrayList<>();

        for (JSONObject chartData : subsidyInformationBottomList) {
            Month.add(chartData.getString("Month"));
            Amount.add(chartData.getString("Application_Amount"));
            Area.add(chartData.getString("Area"));
        }
        subsidyMachineryInformationBottomList.forEach(chartData -> {
            Amount2.add(chartData.getString("Application_Amount"));
            Hu2.add(chartData.getString("Hu_Cnt"));
        });

        JSONObject btxxParams = new JSONObject();
        btxxParams.put("xData", Month);
        btxxParams.put("aAmount", Amount);
        btxxParams.put("aArea", Area);
        btxxParams.put("bAmount", Amount2);
        btxxParams.put("bHouseholds", Hu2);
        List<JSONObject> legend = Lists.newArrayList();
        JSONObject legendData1 = new JSONObject();
        legendData1.put("name", "种植类-补贴金额(万元)");
        legendData1.put("code", "aAmount");
        JSONObject legendData2 = new JSONObject();
        legendData2.put("name", "种植类-补贴面积(亩)");
        legendData2.put("code", "aArea");
        JSONObject legendData3 = new JSONObject();
        legendData3.put("name", "购置类-补贴金额(万元)");
        legendData3.put("code", "bAmount");
        JSONObject legendData4 = new JSONObject();
        legendData4.put("name", "购置类-补贴户数(户)");
        legendData4.put("code", "bHouseholds");

        legend.add(legendData1);
        legend.add(legendData2);
        legend.add(legendData3);
        legend.add(legendData4);

        JSONObject top3 = new JSONObject();
        top3.put("policyCnt", policyCnt + machineryPolicyCnt);
        top3.put("policyArea", policyArea);
        top3.put("mainBodyCnt", mainBodyCnt + mainBodyMachineryCnt);
        jsonObject.put("top3", top3);

        btxxParams.put("legend", legend);
        jsonObject.put("btxxParams", btxxParams);
        return jsonObject;
    }

    @Override
    public JSONObject getMonitorDeviceList(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> deviceList = changeAgricultureMapper.getDeviceCoords("ME0000000000001", townNumber, villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(deviceList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getThreeResourceInformationList(String type, String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> threeResourceInformationList = changeAgricultureMapper.getThreeResourceInformationList(type, townNumber, villageNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(threeResourceInformationList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getNaturalResourcesCoords(String townNumber, String villageNumber, String type) {
        //todo 详情没做呢
        JSONObject jsonObject = new JSONObject();
        if ("补贴区域".equals(type)) {
            //补贴信息
            // 1 如果是 土地种植补贴 则 返回确权地块信息

            List<JSONObject> naturalResourcesPolicy = changeAgricultureMapper.getNaturalResourcesPolicy(townNumber, villageNumber);
            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();

            naturalResourcesPolicy.forEach(nature -> {
                nature.put("pointShape", "Policy");
                // 把这个类的具体信息 也返回
                List<JSONObject> cropList = changeAgricultureMapper.getPolicyType1InfoByCategoryAndLandSerialNumber("SPT000000000001", nature.getString("naturalResourceSerialNumber"));
                if (!CollectionUtils.isEmpty(cropList)) {
                    nature.put("contentList", cropList);
                } else {
                    nature.put("contentList", Lists.newArrayList());
                }


                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });


            // 2 如果是 农机购置补贴 则 返回当前申请人所在的蓝牌地点 point
            List<BluePlateInformation> agriculturalMachineryPolicy = changeAgricultureMapper.getAgriculturalMachineryPolicy(townNumber, villageNumber);
            agriculturalMachineryPolicy.forEach(nature -> {
                nature.setPointShape("Policy");
                // 把这个类的具体信息 也返回
                List<JSONObject> cropList = changeAgricultureMapper.getPolicyType2InfoByCategoryAndLandSerialNumber("SPT000000000002", nature.getBluePlateSerialNumber());
                if (!CollectionUtils.isEmpty(cropList)) {
                    nature.setContentList(cropList);
                } else {
                    nature.setContentList(Lists.newArrayList());
                }

                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getBluePlateSerialNumber());
                JSONObject geometry = new JSONObject();
                geometry.put("type", "point");
                geometry.put("coordinates", nature.getCoord().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.getCoord()) && !nature.getCoord().replace(" ", "").equals("[]]")) {
                    nature.setCoord("");
                    features.add(feature);
                }
            });

            jsonObject.put("features", features);
            return jsonObject;
        }

        if ("畜牧养殖".equals(type)) {// 设备

            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();
            List<JSONObject> deviceList = changeAgricultureMapper.getDeviceCoords("", townNumber, villageNumber);
            deviceList.forEach(device -> {
                device.put("pointShape", "Device");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", device.getString("name"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "point");
                geometry.put("coordinates", device.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", device);
                if (!ObjectUtils.isEmpty(device.get("coords")) && !device.get("coords").toString().replace(" ", "").equals("[]]")) {
                    device.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }

        if ("耕地分布".equals(type)) {//耕地：作物+集体+设备

            // 作物
            List<JSONObject> naturalResourcesPolicy = changeAgricultureMapper.getCropCategoryCoords("", townNumber, villageNumber);
            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();

            naturalResourcesPolicy.forEach(nature -> {
                nature.put("pointShape", "Crop");
                // 把这个类的具体信息 也返回
                List<JSONObject> cropList = changeAgricultureMapper.getCropInfoByCategoryAndLandSerialNumber("", nature.getString("naturalResourceSerialNumber"));
                if (!CollectionUtils.isEmpty(cropList)) {
                    nature.put("contentList", cropList);
                } else {
                    nature.put("contentList", Lists.newArrayList());
                }

                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            // 集体资源用地
            List<JSONObject> naturalResourcesCoords = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", "", "");
            naturalResourcesCoords.forEach(nature -> {
                nature.put("pointShape", "Land");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }

            });
            // 设备
            List<JSONObject> deviceList = changeAgricultureMapper.getDeviceCoords("", townNumber, villageNumber);
            deviceList.forEach(device -> {
                device.put("pointShape", "Device");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", device.getString("name"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "point");
                geometry.put("coordinates", device.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", device);
                if (!ObjectUtils.isEmpty(device.get("coords")) && !device.get("coords").toString().replace(" ", "").equals("[]]")) {
                    device.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;

        }

        if ("林下种植".equals(type)) {

            // 作物
            List<JSONObject> naturalResourcesPolicy = changeAgricultureMapper.getCropCategoryCoords("", townNumber, villageNumber);
            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();

            naturalResourcesPolicy.forEach(nature -> {
                nature.put("pointShape", "Crop");
                // 把这个类的具体信息 也返回
                List<JSONObject> cropList = changeAgricultureMapper.getCropInfoByCategoryAndLandSerialNumber("", nature.getString("naturalResourceSerialNumber"));
                if (!CollectionUtils.isEmpty(cropList)) {
                    nature.put("contentList", cropList);
                } else {
                    nature.put("contentList", Lists.newArrayList());
                }

                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            // 设备
            List<JSONObject> deviceList = changeAgricultureMapper.getDeviceCoords("", townNumber, villageNumber);
            deviceList.forEach(device -> {
                device.put("pointShape", "Device");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", device.getString("name"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "point");
                geometry.put("coordinates", device.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", device);
                if (!ObjectUtils.isEmpty(device.get("coords")) && !device.get("coords").toString().replace(" ", "").equals("[]]")) {
                    device.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }

        if ("特色种植".equals(type)) {
            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();
            List<JSONObject> naturalResourcesCoords = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", "", "NRT000000000007");

            naturalResourcesCoords.forEach(nature -> {
                nature.put("pointShape", "Land");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }

        if ("我在北极有块田".equals(type)) {
            jsonObject.put("type", "FeatureCollection");
            JSONObject crs = new JSONObject();
            crs.put("type", "name");
            JSONObject crsP = new JSONObject();
            crsP.put("name", "EPSG:4490");
            crs.put("properties", crsP);

            jsonObject.put("crs", crs);
            List<JSONObject> features = Lists.newArrayList();
            List<JSONObject> naturalResourcesCoords = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", "", "NRT000000000008");

            naturalResourcesCoords.forEach(nature -> {
                nature.put("pointShape", "Land");
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }
        return jsonObject;
    }

    @Override
    public JSONObject getBeiJiMonitorDeviceList(String townNumber, String villageNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        // 摄像头设备
        String monitoringEquipmentSerialNumber = "ME0000000000001";
        // 北极有块田 标签名
        String naturalResourceTagSerialNumber = "NRT000000000008";
        List<JSONObject> deviceList = changeAgricultureMapper.getBeiJiMonitorDeviceList(townNumber, villageNumber, monitoringEquipmentSerialNumber, naturalResourceTagSerialNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(deviceList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getBeiJiOverview(String townNumber, String villageNumber) {
        JSONObject jsonObject = new JSONObject();
        // 北极有块田 标签名
        String naturalResourceTagSerialNumber = "NRT000000000008";
        // 农用地 类型名
        String naturalResourceTypeSerialNumber = "NRT000000000001";
        int totalCanBePlantAreaCnt = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, "").size();
        int haveAFiledCnt = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber).size();
        BigDecimal haveAFiledCanBeClaimedTotalSCMJ = new BigDecimal("0");
        List<JSONObject> beiJiHaveAFiledCanBeClaimedNatureResource = changeAgricultureMapper.getBeiJiHaveAFiledCanBeClaimedNatureResource(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber, "");
        if (!CollectionUtils.isEmpty(beiJiHaveAFiledCanBeClaimedNatureResource)) {
            for (JSONObject jsonObject1 : beiJiHaveAFiledCanBeClaimedNatureResource) {
                haveAFiledCanBeClaimedTotalSCMJ = haveAFiledCanBeClaimedTotalSCMJ.add(jsonObject1.getBigDecimal("SCMJ"));
            }
        }
        int haveAFiledPlannerCnt = changeAgricultureMapper.getBeiJiHaveAFiledPlannerNatureResource(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber).size();
        jsonObject.put("totalCanBePlantAreaCnt", totalCanBePlantAreaCnt);
        jsonObject.put("haveAFiledAreaCnt", haveAFiledCnt);
        jsonObject.put("haveAFiledCanBeClaimedTotalSCMJ", haveAFiledCanBeClaimedTotalSCMJ);
        jsonObject.put("haveAFiledPlannerCnt", haveAFiledPlannerCnt);
        return jsonObject;
    }

    @Override
    public JSONObject getBeiJiOverviewDetail(String townNumber, String villageNumber, String type, String cropCategorySerialNumber, String ifContract) {
        JSONObject jsonObject = new JSONObject();
        // 北极有块田 标签名
        String naturalResourceTagSerialNumber = "NRT000000000008";
        // 农用地 类型名
        String naturalResourceTypeSerialNumber = "NRT000000000001";
        jsonObject.put("type", "FeatureCollection");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        JSONObject crsP = new JSONObject();
        crsP.put("name", "EPSG:4490");
        crs.put("properties", crsP);

        jsonObject.put("crs", crs);
        List<JSONObject> features = Lists.newArrayList();
        if ("总地块数".equals(type)) {
            List<JSONObject> naturalResourcesCoords = changeAgricultureMapper.getNaturalResourcesCoords(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, "");
            naturalResourcesCoords.forEach(nature -> {
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }
        if ("有块田数量".equals(type)) {
            List<JSONObject> haveAFiled = changeAgricultureMapper.getHaveAFiledCoords(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber, cropCategorySerialNumber);
            haveAFiled.forEach(nature -> {
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }
        if ("可认领作物和土地".equals(type)) {
            List<JSONObject> beiJiHaveAFiledCanBeClaimedNatureResource = changeAgricultureMapper.getBeiJiHaveAFiledCanBeClaimedNatureResource(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber, ifContract);
            beiJiHaveAFiledCanBeClaimedNatureResource.forEach(nature -> {
                JSONObject feature = new JSONObject();
                feature.put("type", "Feature");
                feature.put("id", nature.getString("naturalResourceSerialNumber"));
                JSONObject geometry = new JSONObject();
                geometry.put("type", "Polygon");
                geometry.put("coordinates", nature.get("coords").toString().replace(" ", ""));
                feature.put("geometry", geometry);
                feature.put("properties", nature);
                if (!ObjectUtils.isEmpty(nature.get("coords")) && !nature.get("coords").toString().replace(" ", "").equals("[]]")) {
                    nature.remove("coords");
                    features.add(feature);
                }

            });
            jsonObject.put("features", features);
            return jsonObject;
        }
        if ("种植者数量".equals(type)) {
            List<JSONObject> haveAFiledPlanner = changeAgricultureMapper.getBeiJiHaveAFiledPlannerNatureResource(townNumber, villageNumber, "", naturalResourceTypeSerialNumber, naturalResourceTagSerialNumber);
            haveAFiledPlanner.forEach(planner -> {
                String idNumber = planner.getString("ID_Number");
                List<JSONObject> plannerHouseBluePlateInformation = changeAgricultureMapper.getPlannerHouseBluePlateInformation(idNumber);
                if (!CollectionUtils.isEmpty(plannerHouseBluePlateInformation)) {
                    plannerHouseBluePlateInformation.forEach(bluePlate -> {
                        JSONObject feature = new JSONObject();
                        feature.put("type", "Feature");
                        feature.put("id", bluePlate.getString("Blue_Plate_SerialNumber"));
                        JSONObject geometry = new JSONObject();
                        geometry.put("type", "Point");
                        geometry.put("coordinates", bluePlate.get("Coord").toString().replace(" ", ""));
                        feature.put("geometry", geometry);
                        feature.put("properties", bluePlate);
                        if (!ObjectUtils.isEmpty(bluePlate.get("Coord")) && !bluePlate.get("Coord").toString().replace(" ", "").equals("[]]")) {
                            bluePlate.remove("Coord");
                            features.add(feature);
                        }
                    });
                }
            });
            jsonObject.put("features", features);
            return jsonObject;
        }
        return jsonObject;
    }

    @Override
    public JSONObject getBeiJiHaveAFiledList(String townNumber, String villageNumber, int pageNum, int pageSize) {
        // 北极有块田 标签名
        String naturalResourceTagSerialNumber = "NRT000000000008";
        List<JSONObject> personalPointsAccountList = changeAgricultureMapper.getBeiJiHaveAFiledList(townNumber, villageNumber, naturalResourceTagSerialNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getBeiJiHaveAFiledDetail(String naturalResourceSerialNumber) {
        JSONObject teSeChanYeDetail = changeAgricultureMapper.getBeiJiHaveAFiledDetail(naturalResourceSerialNumber);
        //todo 有块田的单独出 【种植技术支持，生长情况监控，土壤监控状况】

        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotEmpty(teSeChanYeDetail.getString("Land_Expert_SerialNumber"))) {
            JSONObject Planting_Experience = changeAgricultureMapper.getLandExpertBySerialNumber(teSeChanYeDetail.getString("Land_Expert_SerialNumber"));
            jsonObject.put("Planting_Experience", Planting_Experience);
        }
        jsonObject.put("Growth_Conditions_Monitoring", new JSONObject());
        jsonObject.put("Soil_Monitoring_Status", new JSONObject());
        teSeChanYeDetail.put("detail", jsonObject);

        return teSeChanYeDetail;
    }

    @Override
    public JSONObject getCropAreaDistribution(String townNumber, String villageNumber, String year) {
        List<JSONObject> cropAreaDistribution = changeAgricultureMapper.getCropAreaDistributionData(townNumber, villageNumber, year);
        java.util.Map<String, List<JSONObject>> cropMap = cropAreaDistribution.stream().collect(Collectors.groupingBy(jsonObject -> jsonObject.getString("Crop")));

        List<Integer> yearList = Lists.newArrayList();
        LocalDate localDate = LocalDate.now();
        int year1 = localDate.getYear();
        for (int i = 0; i < 5; i++) {
            yearList.add(year1 - i);
        }
        Collections.reverse(yearList);
        List<JSONObject> series = Lists.newArrayList();
        List<String> legend = new ArrayList<>();

        for (Map.Entry<String, List<JSONObject>> entry : cropMap.entrySet()) {
            legend.add(entry.getKey());
            JSONObject jsonObject1 = new JSONObject();
            List<String> data1 = new ArrayList<>();
            List<String> data2 = new ArrayList<>();
            for (Integer xAxi : yearList) {
                List<JSONObject> jsonObjects = entry.getValue();
                for (JSONObject jsonObject : jsonObjects) {
                    if (jsonObject.getInteger("Year").intValue() == xAxi) {
                        data1.add(jsonObject.getString("Year"));
                        data2.add(jsonObject.getString("Area"));
                    } else {
                        data1.add(String.valueOf(xAxi));
                        data2.add("0");
                    }
                }
            }
            jsonObject1.put("Year", data1);
            jsonObject1.put("Area", data2);
            jsonObject1.put("Crop", entry.getKey());
            series.add(jsonObject1);
        }


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("series", series);
        jsonObject.put("xAxis", yearList);
        jsonObject.put("legend", legend);

        return jsonObject;
    }

    @Override
    public List<JSONObject> getCropTypeDistribution(String townNumber, String villageNumber) {
        return changeAgricultureMapper.getCropTypeDistributionData(townNumber, villageNumber);
    }

    @Override
    public List<JSONObject> getCropCategory() {
        return changeAgricultureMapper.getCropCategory();
    }

    @Override
    public List<JSONObject> getMonitorDeviceRealTimeList(String natureResourceSerialNumber) {
        return changeAgricultureMapper.getMonitorDeviceRealTimeList(natureResourceSerialNumber);
    }
}
